package com.yztc.love_travel.ui.mainservice.buy.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yztc.love_travel.R;
import com.yztc.love_travel.adater.BuyingAdapter;
import com.yztc.love_travel.bean.BuyingBean;
import com.yztc.love_travel.ui.mainservice.buy.presenter.BuyingPresenterImpl;
import com.yztc.love_travel.ui.mainservice.buy.presenter.IBuyingPresenter;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "欢乐购"
 */
public class BuyActivity extends AppCompatActivity implements IBuyingView {

    private IBuyingPresenter mPresenter;
    @BindView(R.id.buy_toolbar)
    Toolbar buyToolbar;
    @BindView(R.id.listview_buy)
    ListView listviewBuy;
    @BindView(R.id.activity_buy)
    LinearLayout activityBuy;

    private List<BuyingBean.ManualArticleListBean> manualleList=new ArrayList<>();
    private BuyingAdapter mBuyingAdapter;
    private Context mContext=this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy);
        ButterKnife.bind(this);

        initPresenter();
        loadData();
        initToolBar();
        initAdapter();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home)
        {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void loadData() {
        Map<String,Object> params = new HashMap<>();
        params.put(URLConstant.Buying.PAGENO,0);
        params.put(URLConstant.Buying.PAGESIZE,20);
        params.put(URLConstant.Buying.MANUALCATEID,3);
        params.put(URLConstant.Buying.STATUS,1);
        mPresenter.getBuyingBean(URLConstant.Buying.URL_BUYING_BASE,params);
    }

    private void initPresenter() {
        mPresenter=new BuyingPresenterImpl(this);
    }

    private void initAdapter() {
        mBuyingAdapter=new BuyingAdapter(manualleList,mContext);
        listviewBuy.setAdapter(mBuyingAdapter);
        listviewBuy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(BuyActivity.this, BuyDetailActivity.class);
                int articleId =manualleList.get(position).getArticleId();
                intent.putExtra(OtherConstant.KEY_BUYING_ID,articleId);
                startActivity(intent);
            }
        });
    }

    private void initToolBar() {
        buyToolbar.setTitle(R.string.buy);
        setSupportActionBar(buyToolbar);
        ActionBar action=getSupportActionBar();
        action.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void loadStart() {

    }

    @Override
    public void loadFinish() {

    }

    @Override
    public void loadError() {

    }

    @Override
    public void showBuyingData(BuyingBean buyingBean) {

        List<BuyingBean.ManualArticleListBean> manualArticleList = buyingBean.getManualArticleList();
        manualleList.addAll(manualArticleList);
        mBuyingAdapter.notifyDataSetChanged();
    }

}
